<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="分类名称" prop="flName">
        <el-input
          v-model="queryParams.flName"
          placeholder="请输入分类名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属店铺的名称" prop="flDpId">
<!--        <el-input-->
<!--          v-model="queryParams.flDpId"-->
<!--          placeholder="请输入所属店铺的ID"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
        <el-select v-model="queryParams.flDpId" placeholder="请选择下拉选择" clearable :style="{width: '100%'}">
          <el-option v-for="(item, index) in flDpList" :key="index" :label="item.dpName" :value="item.dpId" :disabled="item.disabled"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="daterangeFlAddtime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="审核该分类的状态" prop="flShId">
<!--        <el-input-->
<!--          v-model="queryParams.flShId"-->
<!--          placeholder="请输入审核该分类的状态"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
        <el-select v-model="queryParams.flShId" placeholder="请选择下拉选择" clearable :style="{width: '100%'}">
          <el-option v-for="(item, index) in shenhelist" :key="index" :label="item.spshName" :value="item.spshId" :disabled="item.disabled"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:fenlei:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:fenlei:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:fenlei:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:fenlei:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="fenleiList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="分类ID" align="center" prop="flId" />
      <el-table-column label="分类名称" align="center" prop="flName" />
      <el-table-column label="分类描述" align="center" prop="flMiaoshu" />
      <el-table-column label="所属店铺的名称" align="center" prop="dianpu.dpName" />
      <el-table-column label="创建时间" align="center" prop="flAddtime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.flAddtime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否是可用的分类" align="center" prop="flIsuser" />
      <el-table-column label="审核该分类的状态" align="center" prop="spshenhe.spshName" />
      <el-table-column label="操作" align="center" prop="flCaozuo" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:fenlei:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:fenlei:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品分类对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="分类名称" prop="flName">
          <el-input v-model="form.flName" placeholder="请输入分类名称" />
        </el-form-item>
        <el-form-item label="分类描述" prop="flMiaoshu">
          <el-input v-model="form.flMiaoshu" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="所属店铺的名称" prop="flDpId">
<!--          <el-input v-model="form.flDpId" placeholder="请输入所属店铺的ID" />-->
          <el-select v-model="form.flDpId" placeholder="请选择下拉选择" clearable :style="{width: '100%'}">
            <el-option v-for="(item, index) in flDpList" :key="index" :label="item.dpName" :value="item.dpId" :disabled="item.disabled"></el-option>
          </el-select>
        </el-form-item>
        <el-divider content-position="center">审核原因信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddShenheyuanyin">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteShenheyuanyin">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="shenheyuanyinList" :row-class-name="rowShenheyuanyinIndex" @selection-change="handleShenheyuanyinSelectionChange" ref="shenheyuanyin">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="审核原因名称" prop="shyyName" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.shyyName" placeholder="请输入审核原因名称" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFenlei, getFenlei, delFenlei, addFenlei, updateFenlei } from "@/api/system/fenlei";
import {listDianpu, listSpshenhe} from "@/api/shangpin/shangpin";

export default {
  name: "Fenlei",
  data() {
    return {
      // 下拉审核
      shenhelist:[{
        "spshName": "选项一",
        "spshId": 1
      }, {
        "spshName": "选项二",
        "spshId": 2
      }],
      //店铺下拉选择
      flDpList:[{
        "dpName": "选项一",
        "dpId": 1
      }, {
        "dpName": "选项二",
        "dpId": 2
      }],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedShenheyuanyin: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品分类表格数据
      fenleiList: [],
      // 审核原因表格数据
      shenheyuanyinList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 审核原因名称时间范围
      daterangeFlAddtime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        flName: null,
        flDpId: null,
        flAddtime: null,
        flShId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        flName: [
          { required: true, message: "分类名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getShenHeList();
    this.getflDpList();
  },
  methods: {
    //店铺下拉
    getflDpList(){
      listDianpu(null).then(response =>{
        this.flDpList = response.rows;
      })
    },
    //审核下拉框
    getShenHeList(){
      listSpshenhe(null).then(response =>{
        this.shenhelist = response.rows;
      })
    },
    /** 查询商品分类列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeFlAddtime && '' != this.daterangeFlAddtime) {
        this.queryParams.params["beginFlAddtime"] = this.daterangeFlAddtime[0];
        this.queryParams.params["endFlAddtime"] = this.daterangeFlAddtime[1];
      }
      listFenlei(this.queryParams).then(response => {
        this.fenleiList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        flId: null,
        flName: null,
        flMiaoshu: null,
        flDpId: null,
        flAddtime: null,
        flIsuser: null,
        flShId: null,
        flCaozuo: null,
        flYuanyinId: null,
        flJujue: null
      };
      this.shenheyuanyinList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeFlAddtime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.flId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品分类";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const flId = row.flId || this.ids
      getFenlei(flId).then(response => {
        this.form = response.data;
        this.shenheyuanyinList = response.data.shenheyuanyinList;
        this.open = true;
        this.title = "修改商品分类";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.shenheyuanyinList = this.shenheyuanyinList;
          if (this.form.flId != null) {
            updateFenlei(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFenlei(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const flIds = row.flId || this.ids;
      this.$modal.confirm('是否确认删除商品分类编号为"' + flIds + '"的数据项？').then(function() {
        return delFenlei(flIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 审核原因序号 */
    rowShenheyuanyinIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 审核原因添加按钮操作 */
    handleAddShenheyuanyin() {
      let obj = {};
      obj.shyyName = "";
      this.shenheyuanyinList.push(obj);
    },
    /** 审核原因删除按钮操作 */
    handleDeleteShenheyuanyin() {
      if (this.checkedShenheyuanyin.length == 0) {
        this.$modal.msgError("请先选择要删除的审核原因数据");
      } else {
        const shenheyuanyinList = this.shenheyuanyinList;
        const checkedShenheyuanyin = this.checkedShenheyuanyin;
        this.shenheyuanyinList = shenheyuanyinList.filter(function(item) {
          return checkedShenheyuanyin.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleShenheyuanyinSelectionChange(selection) {
      this.checkedShenheyuanyin = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/fenlei/export', {
        ...this.queryParams
      }, `fenlei_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
